package com.hzp.micromall.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzp.micromall.admin.dao.UmsMenuDao;
import com.hzp.micromall.admin.dto.UmsMenuNode;
import com.hzp.micromall.admin.entity.UmsMenu;
import com.hzp.micromall.admin.service.UmsMenuService;
import com.hzp.micromall.common.core.api.CommonPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author houzipeng
 * @Date 2022/2/25 10:13
 * @Version 1.0
 * 后台菜单管理Service实现类
 */
@Service
public class UmsMenuServiceImpl implements UmsMenuService {

    @Autowired
    private UmsMenuDao menuDao;

    @Override
    public int createMenu(UmsMenu umsMenu) {
        umsMenu.setCreateTime(new Date());
        updateMenuLevel(umsMenu);
        return menuDao.insert(umsMenu);
    }

    /**
     * 修改菜单层级
     */
    private void updateMenuLevel(UmsMenu umsMenu) {
        if (umsMenu.getParentId() == 0) {
            //没有父菜单时为一级菜单
            umsMenu.setLevel(0);
        } else {
            //有父菜单时选择根据父菜单level设置
            UmsMenu parentMenu = menuDao.selectById(umsMenu.getParentId());
            if (parentMenu != null) {
                umsMenu.setLevel(parentMenu.getLevel() + 1);
            } else {
                umsMenu.setLevel(0);
            }
        }
    }

    @Override
    public int updateMenuById(Long id, UmsMenu umsMenu) {
        umsMenu.setId(id);
        updateMenuLevel(umsMenu);
        return menuDao.updateById(umsMenu);
    }

    @Override
    public UmsMenu getMenuDetail(Long id) {
        return menuDao.selectById(id);
    }

    @Override
    public int deleteMenuById(Long id) {
        return menuDao.deleteById(id);
    }

    @Override
    public CommonPage<UmsMenu> getMenuPageList(Long parentId, Integer pageSize, Integer pageNum) {
        Page<UmsMenu> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getParentId, parentId)
                .orderByDesc(UmsMenu::getSort);
        menuDao.selectPage(page, wrapper);
        return new CommonPage<>(page.getCurrent(), page.getTotal(), page.getSize(), page.getRecords());
    }

    @Override
    public List<UmsMenuNode> getTreeMenuList() {
        List<UmsMenu> menuList = menuDao.selectList(null);
        List<UmsMenuNode> result = menuList.stream()
                .filter(menu -> menu.getParentId().equals(0L))
                .map(menu -> covertMenuNode(menu, menuList)).collect(Collectors.toList());
        return result;
    }

    /**
     * 将UmsMenu转化为UmsMenuNode并设置children属性
     */
    private UmsMenuNode covertMenuNode(UmsMenu menu, List<UmsMenu> menuList) {
        UmsMenuNode node = new UmsMenuNode();
        BeanUtil.copyProperties(menu, node);
        List<UmsMenuNode> children = menuList.stream()
                .filter(subMenu -> subMenu.getParentId().equals(menu.getId()))
                .map(subMenu -> covertMenuNode(subMenu, menuList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }

    @Override
    public int updateMenuHidden(Long id, Integer hidden) {
        UmsMenu umsMenu = new UmsMenu();
        umsMenu.setId(id);
        umsMenu.setHidden(hidden);
        return menuDao.updateById(umsMenu);
    }
}
